package cn.itrip.controller;

import cn.itrip.entity.ItripUser;
import cn.itrip.entity.ItripUserLinkUser;
import cn.itrip.service.ItripOrderLinkUserService;
import cn.itrip.service.ItripUserLinkUserService;
import cn.itrip.service.TokenService;
import cn.itrip.vo.R;
import cn.itrip.vo.biz.ItripSearchUserLinkUserVO;
import cn.itrip.vo.biz.UserLinkUserVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/api/userinfo")
public class UserInfoController {

    @Resource
    private ItripUserLinkUserService itripUserLinkUserService;
    @Resource
    private ItripOrderLinkUserService itripOrderLinkUserService;
    @Resource
    private TokenService tokenService;

    @ApiOperation(value = "修改联系人")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK") })
    @PostMapping(value = "/modifyuserlinkuser",headers = {"token"})
    public R modifyUserLinkUser(@RequestBody UserLinkUserVO userLinkUserVO,HttpServletRequest request){
        String tokenString = request.getHeader("token");
        ItripUser currentUser = tokenService.load(tokenString);
        System.out.println(userLinkUserVO.getId());
        ItripUserLinkUser itripUserLinkUser = itripUserLinkUserService.getUserLinkUserById(userLinkUserVO.getId());
        System.out.println(itripUserLinkUser.toString());
        if (userLinkUserVO.getLinkIdCard()!=null){
            itripUserLinkUser.setLinkIdCard(userLinkUserVO.getLinkIdCard());
        }
        if (userLinkUserVO.getLinkIdCardType()!=null){
            itripUserLinkUser.setLinkIdCardType(userLinkUserVO.getLinkIdCardType());
        }
        if (userLinkUserVO.getLinkPhone()!=null){
            itripUserLinkUser.setLinkPhone(userLinkUserVO.getLinkPhone());
        }
        if (userLinkUserVO.getLinkUserName()!=null){
            itripUserLinkUser.setLinkUserName(userLinkUserVO.getLinkUserName());
        }
        itripUserLinkUser.setModifyDate(new Date());
        itripUserLinkUser.setModifiedBy(currentUser.getId());
        if (itripUserLinkUserService.saveOrUpdate(itripUserLinkUser)){
            return R.success("修改常用联系人成功");
        }
        return R.fail("修改常用联系人失败");
    }

    @ApiOperation(value = "删除联系人")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK") })
    @GetMapping("/deluserlinkuser")
    public R delUserLinkUser(Integer id){
        if(itripOrderLinkUserService.getOrderLinkUserOrderStatusBylinkUserId(id)==null){
            itripUserLinkUserService.delUserLinkUserById(id);
            return R.success("删除常用联系人成功");
        }
        R r = new R();
        r.setErrorCode("100431");
        r.setMsg("所选的常用联系人中有与某条待支付的订单关联的项，无法删除");
        r.setSuccess("false");
        return r;
    }

    @ApiOperation(value = "添加常用联系人")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK") })
    @PostMapping(value = "/adduserlinkuser",headers = {"token"})
    public R addUserLinkUser(@RequestBody UserLinkUserVO userLinkUserVO,HttpServletRequest request){
        String tokenString = request.getHeader("token");
        ItripUser currentUser = tokenService.load(tokenString);
        ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
        BeanUtils.copyProperties(userLinkUserVO,itripUserLinkUser);
        itripUserLinkUser.setCreationDate(new Date());
        itripUserLinkUser.setCreatedBy(currentUser.getId());
        Integer UserId =  currentUser.getId().intValue();
        itripUserLinkUser.setUserId(UserId);
        itripUserLinkUserService.save(itripUserLinkUser);
        return R.success("新增常用联系人成功");
    }

    @ApiOperation(value = "查询用户联系人列表")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK") })
    @PostMapping("/queryuserlinkuser")
    public R queryUserLinkUser(@RequestBody ItripSearchUserLinkUserVO itripSearchUserLinkUserVO, HttpServletRequest request){
        String tokenString = request.getHeader("token");
        ItripUser currentUser = tokenService.load(tokenString);
        List<ItripUserLinkUser> userLinkUserList = new ArrayList<ItripUserLinkUser>();
        String linkUserName = (null == itripSearchUserLinkUserVO) ? null : itripSearchUserLinkUserVO.getLinkUserName();
        if (null != currentUser) {
            Long userId = currentUser.getId();
            try {
                userLinkUserList = itripUserLinkUserService.getItripUserLinkUserListByMap(userId,linkUserName);
                return R.success("获取常用联系人信息成功", userLinkUserList);
            } catch (Exception e) {
                e.printStackTrace();
                R r = new R();
                r.setErrorCode("100401");
                r.setMsg("获取常用联系人信息失败");
                return r;
            }
        } else {
            R r = new R();
            r.setErrorCode("100000");
            r.setMsg("token失效，请重新登录");
            return r;
        }
    }
}
